package com.qbw.app.module.common.util;

import androidx.annotation.StringRes;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.lib.util.ResUtil;
import com.qbw.app.R;
import com.qbw.app.XApplication;
import com.qbw.l.L;

import xyz.bboylin.universaltoast.R.id;

import java.lang.reflect.Field;

import xyz.bboylin.universialtoast.UniversalToast;

/**
 * @author qinbaowei
 * @date 2018/1/27
 * @email qbaowei@qq.com
 */

public class UniversalToastUtil {

    public static void showToast(@StringRes int message) {
        showToast(ResUtil.getString(message));
    }

    public static void showToast(String message) {
        showToast(message, UniversalToast.LENGTH_SHORT);
    }

    public static void showToast(String message, int duration) {
        if (TextUtils.isEmpty(message)) {
            return;
        }
        UniversalToast toast = UniversalToast.makeText(XApplication.getInstance(),
                                                       message,
                                                       duration,
                                                       UniversalToast.EMPHASIZE)
                                             .setGravity(Gravity.CENTER, 0, 0)
                                             .setBackground(ResUtil.getDrawable(R.drawable.toast_bg));
        setTextSize(toast);
        toast.show();
    }

    public static void showRightIconToast(int message) {
        showRightIconToast(XApplication.getInstance().getResources().getString(message));
    }

    public static void showRightIconToast(String message) {
        UniversalToast.makeText(XApplication.getInstance(),
                                message,
                                UniversalToast.LENGTH_SHORT,
                                UniversalToast.EMPHASIZE)
                      .setGravity(Gravity.CENTER, 0, 0)
                      .setBackground(ResUtil.getDrawable(R.drawable.toast_bg))
                      .setIcon(R.drawable.toast_success)// 设置icon，未调用的话icon不可见(gone)
                      .show();
    }

    public static void showFailedIconToast(int message) {
        showFailedIconToast(XApplication.getInstance().getResources().getString(message));
    }

    public static void showFailedIconToast(String message) {
        UniversalToast.makeText(XApplication.getInstance(),
                                message,
                                UniversalToast.LENGTH_SHORT,
                                UniversalToast.EMPHASIZE)
                      .setGravity(Gravity.CENTER, 0, 0)
                      .setBackground(ResUtil.getDrawable(R.drawable.toast_bg))
                      .setIcon(R.drawable.toast_failed)// 设置icon，未调用的话icon不可见(gone)
                      .show();
    }

    private static void setTextSize(UniversalToast toast) {
        try {
            Field field = toast.getClass().getDeclaredField("toast");
            field.setAccessible(true);
            Toast t = (Toast) field.get(toast);
            View view = t.getView();
            TextView textView = view.findViewById(id.text);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
                                 ResUtil.getDimension(R.dimen.toast_text_size));
        } catch (Exception e) {
            e.printStackTrace();
            L.GL.e(e);
        }
    }
}
